﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using com.fmtx.IDal;
using com.fmtx.Model;
using fmtx.Framework.DALFactory;
using fmtx.Framework;
using System.Collections;
using fmtx.Utility;

namespace website.Areas.Action.Controllers
{
    public class CommentController : BaseController
    {
        //
        // GET: /Action/Comment/
        IComment comment = DataAccess<IComment>.GetObject();

        /// <summary>
        /// 发布文章评论
        /// </summary>
        /// <param name="id"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        [IsLoginJson]
        public ActionResult DoCommentA(int id, string content)
        {
            content = content.RemoveUnsafeHtml().FileDirt();
            return Json(new { Id = doComment(id, content, Target.ARTICLE), Uid = userInfo.Uid, Uname = userInfo.Name, Header = userInfo.Header, Content= content });
        }

        /// <summary>
        /// 发布图片评论
        /// </summary>
        /// <param name="id"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        [IsLoginJson]
        public ActionResult DoCommentP(int id, string content)
        {
            content = content.RemoveUnsafeHtml().FileDirt();
            return Json(new { Id = doComment(id, content, Target.PICTRUE), Uid = userInfo.Uid, Uname = userInfo.Name, Header = userInfo.Header, Content = content });
        }

        /// <summary>
        /// 保存评论
        /// </summary>
        /// <param name="id"></param>
        /// <param name="content"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        [NonAction]
        private int doComment(int id, string content, Target target)
        {
            try
            {
                Comment model = new Comment();
                model.TagType = target;
                model.TagId = id;
                model.InTime = DateTime.Now;
                model.Content = content.FileDirt();
                model.Uid = userInfo.Uid;
                model.Ip = Request.UserHostAddress;
                bool istrue= comment.Add(model);
                if (istrue)
                {
                    switch (target)
                    {
                        case Target.ARTICLE:
                            ArticleCache.InsertComment(model);
                            IArticle article = DataAccess<IArticle>.GetObject();
                            var abean = article.Get(id);
                            abean.CommentCount++;
                            article.Update(abean);
                            break;
                        case Target.PICTRUE:
                            PictureCache.InsertComment(model);
                            IPicture picture = DataAccess<IPicture>.GetObject();
                            var pbean = picture.Get(id);
                            pbean.CommentCount++;
                            picture.Update(pbean);
                            break;
                        case Target.COMMENT:
                            break;
                        default:
                            break;
                    }
                    return model.Id;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return 0;
            }

        }

    }
}
